#include "CocosPCH.h"
#include "CoCElementMovieClip.h"
#include "CoCSWF.h"
#include <assert.h>

#include "Utility.h"

CoCElementMovieClip::CoCElementMovieClip(void)
{
}


CoCElementMovieClip::~CoCElementMovieClip(void)
{
}

bool CoCElementMovieClip::Parse(void *data, CoCSWF *pSWF)
{		
	int nDataPos = 0;

	Utility_ReadValue(&m_nID, data, &nDataPos);	
	Utility_ReadValue(&m_cFrameRate, data, &nDataPos);
	Utility_ReadValue(&m_nFrameCount, data, &nDataPos);	
	unsigned short nFrameInfoCount;
	Utility_ReadValue(&nFrameInfoCount, data, &nDataPos);
	nDataPos+=2;

	m_vecFrameInfo.resize(nFrameInfoCount);

	for(int i = 0; i < nFrameInfoCount; i++)
	{
		FrameInfo &info = m_vecFrameInfo[i];	
		Utility_ReadValue(&info.nShapeIndex, data, &nDataPos);
		
		unsigned short nIndex;
		Utility_ReadValue(&nIndex, data, &nDataPos);
		if(pSWF->GetTrans(nIndex))
			info.trans = pSWF->GetTrans(nIndex)->GetMatrix();
		else
			info.trans = NULL;		
		Utility_ReadValue(&nIndex, data, &nDataPos);
		if(pSWF->GetColor(nIndex))
			info.color = pSWF->GetColor(nIndex)->GetColor();
		else
			info.color = NULL;
	}

	unsigned short nCount;
	Utility_ReadValue(&nCount, data, &nDataPos);
	if(nCount > 0)
	{
		m_vecShapeID.resize(nCount);
		Utility_ReadData(&m_vecShapeID[0], data, nCount*sizeof(unsigned short), &nDataPos);
		for(int i = 0; i < nCount; i++)
		{
			int nID = m_vecShapeID[i];;
			nID = nID;
		}
	}

	nDataPos+=nCount;

	for(int i = 0; i < nCount; i++)
	{
		char cNameLength;
		Utility_ReadValue(&cNameLength, data, &nDataPos);
		if(cNameLength != -1)
		{
			nDataPos+=cNameLength;
		}
	}
	
	for(int i = 0; i < m_nFrameCount; i++)
	{
		nDataPos += 1;
		int nLength;
		Utility_ReadValue(&nLength, data, &nDataPos);
		unsigned short nNum;
		Utility_ReadValue(&nNum, data, &nDataPos);
		m_vecAnimationInfo.push_back(nNum);
		nDataPos -= 2;
		nDataPos += nLength;
	}

		
	return true;
}

unsigned short CoCElementMovieClip::GetID()
{
	return m_nID;
}

std::vector<unsigned short> &CoCElementMovieClip::GetShapes()
{
	return m_vecShapeID;
}
char CoCElementMovieClip::GetFrameRate()
{
	return m_cFrameRate;
}
unsigned short CoCElementMovieClip::GetFrameCount()
{
	return m_nFrameCount;
}

std::vector<FrameInfo> &CoCElementMovieClip::GetFrameInfo()
{
	return m_vecFrameInfo;
}

std::vector<unsigned short> &CoCElementMovieClip::GetAnimationInfo()
{
	return m_vecAnimationInfo;
}